package com.example.dirviesystem.coach.controller;

import com.example.dirviesystem.entity.CoachInfo;
import com.example.dirviesystem.entity.CoachQualification;
import com.example.dirviesystem.coach.service.CoachInfoService;
import com.example.dirviesystem.coach.service.CoachQualificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/qualifications")
@CrossOrigin(origins = "http://localhost:5173") // 允许前端请求
public class CoachQualificationController {
    @Autowired
    private CoachInfoService coachInfoService;


    @Autowired
    private CoachQualificationService coachQualificationService;

    @PostMapping("/add")
    public ResponseEntity<CoachQualification> addQualification(@RequestBody CoachQualification qualification) {
        System.out.println(qualification);
        qualification.setApproved(false); // 初始状态为 false

        // 查找教练信息并设置
        CoachInfo coach = coachInfoService.findById(qualification.getCoach().getId());
        if (coach != null) {
            qualification.setCoach(coach);
            CoachQualification savedQualification = coachQualificationService.save(qualification);
            return ResponseEntity.ok(savedQualification);
        } else {
            return ResponseEntity.badRequest().body(null); // 处理教练不存在的情况
        }
    }
}